﻿// MainForm.cs
// (c) 2011, Charles Lechasseur
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using LaunchWithParams.Properties;

namespace LaunchWithParams
{
    //************************************************************************
    /// <summary>
    /// Main form of the LaunchWithParams application.
    /// </summary>
    //************************************************************************
    public partial class MainForm : Form
    {
        /// Settings for the executable (most-recently-used command-lines, etc.)
        private ExecutableSettings settings;

        //************************************************************************
        /// <summary>
        /// Constructor.
        /// </summary>
        //************************************************************************
        public MainForm()
        {
            InitializeComponent();
        }

        //************************************************************************
        /// <summary>
        /// Called when the form is first loaded. We populate our controls here.
        /// </summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="e">Event arguments.</param>
        //************************************************************************
        private void MainForm_Load(object sender, EventArgs e)
        {
            // The form's title is set to a format string where
            // we need to plug in the name of the application to launch.
            // We'll remove the path portion and display it.
            this.Text = String.Format(this.Text, Path.GetFileName(ApplicationSettings.ExeToLaunch));

            // Load settings for the executable.
            settings = new ExecutableSettings(ApplicationSettings.ExeToLaunch);

            // Populate the dropdown with the previous command-lines.
            CmdLineCombo.Items.AddRange(settings.MostRecentlyUsedCommandLines);
            if (settings.PinnedCommandLines.Length != 0) {
                if (settings.MostRecentlyUsedCommandLines.Length != 0) {
                    CmdLineCombo.Items.Add("-");
                }
                CmdLineCombo.Items.AddRange(settings.PinnedCommandLines);
            }
        }

        //************************************************************************
        /// <summary>
        /// Called when the user chooses a command-line in the drop-down list. This
        /// corresponds to either a command-line in the list of most-recently used
        /// command-lines, or a pinned command-line. Whatever happens, we need to
        /// update the state of the "pinned" check box.
        /// </summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="e">Event arguments.</param>
        //************************************************************************
        private void CmdLineCombo_SelectedValueChanged(object sender, EventArgs e)
        {
            // Determine if this is a pinned command-line or not.
            object selection = CmdLineCombo.SelectedItem;
            PinnedChk.Checked = (selection is CommandLineInfo && ((CommandLineInfo) selection).IsPinned);
        }

        //************************************************************************
        /// <summary>
        /// Called when the Cancel button is pressed. We simply close.
        /// </summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="e">Event arguments.</param>
        //************************************************************************
        private void CancelBtn_Click(object sender, EventArgs e)
        {
            // Simply close the form.
            Close();
        }

        //************************************************************************
        /// <summary>
        /// Called when the OK button is pressed. We need to launch the executable
        /// here, using the arguments provided by the user.
        /// </summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="e">Event arguments.</param>
        //************************************************************************
        private void OKBtn_Click(object sender, EventArgs e)
        {
            // Get command-line to use from combo box.
            string cmdLine = CmdLineCombo.Text;

            // Update settings unless user chose the marker "-" item.
            if (CmdLineCombo.SelectedIndex != -1 && CmdLineCombo.SelectedItem is CommandLineInfo) {
                // Either pin or unpin the command-line. If "pin" is checked, it can mean
                // two things:
                // 1. The user selected a pinned command-line in the list.
                // 2. The user entered a new command-line and chose "pin".
                //
                // If the checkbox is not checked, it can mean two things:
                // 1. The user entered a command-line by hand
                // 2. The user selected a pinned command-line and unchecked "pin".
                //
                // Whatever the cause, we can try to pin or unpin the command-line.
                // The methods will take care of validating that the command-line is really
                // in the list or not before proceeding.
                if (PinnedChk.Checked) {
                    settings.PinNewCommandLine(cmdLine);
                } else {
                    settings.UnpinCommandLine(cmdLine);
                }
            }

            // Save used command-line parameters in the settings.
            // We do this even for pinned command-lines.
            settings.AddUsedCommandLine(cmdLine);

            // Launch executable using the provided command-line parameters.
            Process.Start(ApplicationSettings.ExeToLaunch, cmdLine);

            // Save settings, then close the form.
            settings.SaveData();
            Close();
        }
    }
}
